package com.yang.sso.oauth.oauth.enhancer;

import cn.hutool.core.map.MapUtil;
import com.yang.sso.oauth.model.MyUserDetails;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.Map;

/**
 * jwt 自定义Token增强
 * TODO  存在问题
 * @author: Islands
 * @create: 2024-04-11 22:00
 */

@Configuration
public class MyTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
        Object principal = oAuth2Authentication.getUserAuthentication().getPrincipal();
        Map<String, Object> map = MapUtil.newHashMap();
        if (principal instanceof MyUserDetails) {
            MyUserDetails user = (MyUserDetails) principal;
            map.put("username",user.getUsername());
        }
        ((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(map);
        return oAuth2AccessToken;
    }
}
